package com.ray.controller;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.WorkOrderMachineBean;
import com.ray.pojo.MyUser;
import com.ray.service.ProjectService;
import com.ray.service.WeiXinService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.WeiXinParamesUtil;
import com.ray.util.WeiXinUtil;

@Controller
@RequestMapping("/project")
public class ProjectController {
	
	@Autowired
	ProjectService projectService;
	
	@Autowired
	WeiXinService weiXinService;
	//工程信息配置
	@RequestMapping("/saveInfo")
	@ResponseBody
	public Map saveInfo(HttpServletRequest request){
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(projectService.saveInfo(map));
	}
	//工程获取信息
	@RequestMapping("/getInfo")
	@ResponseBody
	public Map getInfo(HttpServletRequest request){
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(projectService.getInfo(map));
	}
	
	/*@RequestMapping("/sqr")
	public String  sh(HttpServletRequest request,String redictUrl) throws UnsupportedEncodingException {
		StringBuffer url = request.getRequestURL();  
		String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getServletContext().getContextPath()).append("/").toString();
		return "redirect:http://www.shinehao.net/weixin/wxauth.html?appid="+WeiXinParamesUtil.weiXinPbulicNumberAppId+"&scope=snsapi_base&state="+redictUrl+"&redirect_uri="+tempContextUrl+"project/sqrInfo";
	}
	
	@RequestMapping("/sqrInfo")
	public String  sqrInfo(String code,String state,Model model,HttpServletRequest request) throws UnsupportedEncodingException {
		MyUser myUser=weiXinService.getWeiXinPublicNumberReportUser(code,request);
		if(state!=null && !"".equals(state)) {
			String accessToken=weiXinService.getAccessToken(null);
			boolean subscribe = WeiXinUtil.hasSubscribe(accessToken, myUser.getWeiXinPublicOpenId());
			if(!subscribe || null==myUser.getWeiXinPublicOpenId()||"".equals(myUser.getWeiXinPublicOpenId())) {
				return "redirect:/WeChatQrcode.html";//先去关注
			}else {
				if(null==myUser.getUserName()||"".equals(myUser.getUserName())) {
					WorkOrderMachineBean bean = new WorkOrderMachineBean();
					myUser.setCompany("无账号公司");
					myUser.setUserName("无账号用户");
					myUser.setPhone("");
					myUser.setDeptId("0");
					myUser.setUserId("-1");
					request.getSession().setAttribute("userLogin", myUser);
				}
				return "redirect:/"+state;
			}
		}else {
				model.addAttribute("weiXinPublicOpenId", myUser.getWeiXinPublicOpenId());
				return "redirect:/login.jsp";
		}
	}*/

	
	
}
